package com.app.newiptv.helper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "save_object.db";//数据库文件名
    private static SQLiteDatabase WRITE_INSTANCE;
    private static SQLiteDatabase READ_INSTANCE;
    private Context mContext;
    private static MySQLiteOpenHelper instance = null;
    public MySQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public MySQLiteOpenHelper(Context context) {
        this(context, DB_NAME, null, 1);
        this.mContext=context;
    }
    public static MySQLiteOpenHelper getInstance(Context context) {
        if (instance == null) {
            instance = new MySQLiteOpenHelper(context);
        }
        return instance;
    }
    public SQLiteDatabase getWriteInstance() {
        if (WRITE_INSTANCE == null) {
            WRITE_INSTANCE = new MySQLiteOpenHelper(mContext).getWritableDatabase();
        }
        return WRITE_INSTANCE;
    }

    public SQLiteDatabase getReadInstance() {
        if (READ_INSTANCE == null) {
            READ_INSTANCE = new MySQLiteOpenHelper(mContext).getReadableDatabase();
        }
        return READ_INSTANCE;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 0为收藏 1为观看记录
        String create_sql = "create table if not exists RecodeCollect(" +
                "id integer primary key  autoincrement, pic_url text not null," +
                "type int not null, name varchar(20) not null,actor text not null, movie_id int not null, movie_type int not null, sitcom_index int, play_time int, collect_time bigint)";
        db.execSQL(create_sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
